<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>知识库文件上传</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; }
        .container { border: 1px solid #ddd; padding: 20px; border-radius: 5px; }
        .form-group { margin-bottom: 15px; }
        label { display: block; margin-bottom: 5px; }
        input[type="file"] { margin-bottom: 10px; }
        button { background-color: #4CAF50; color: white; padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; }
        button:hover { background-color: #45a049; }
        #result { margin-top: 20px; padding: 10px; border-radius: 4px; }
        .success { background-color: #dff0d8; color: #3c763d; }
        .error { background-color: #f2dede; color: #a94442; }
    </style>
</head>
<body>
    <div class="container">
        <h1>知识库文件上传</h1>
            <p>请上传任意txt格式的文件作为知识库内容</p>
        <form id="uploadForm">
            <div class="form-group">
                <label for="file">选择文件：</label>
                <input type="file" id="file" name="file" accept=".txt">
            </div>
            <button type="submit">上传</button>
        </form>
        <div id="result"></div>
    </div>

    <script>
        document.getElementById('uploadForm').addEventListener('submit', function(e) {
            e.preventDefault();
            const fileInput = document.getElementById('file');
            const resultDiv = document.getElementById('result');

            if (!fileInput.files || fileInput.files.length === 0) {
                resultDiv.className = 'error';
                resultDiv.textContent = '请选择一个文件';
                return;
            }

            const file = fileInput.files[0];
            // 允许上传任意txt文件
            if (!file.name.endsWith('.txt')) {
                resultDiv.className = 'error';
                resultDiv.textContent = '请上传txt格式的文件';
                return;
            }

            const formData = new FormData();
            formData.append('file', file);

            fetch('http://localhost:5000/upload', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.status === 'success') {
                    resultDiv.className = 'success';
                    resultDiv.textContent = '文件上传成功！路径：' + data.path;
                } else {
                    resultDiv.className = 'error';
                    resultDiv.textContent = '上传失败：' + data.error;
                }
            })
            .catch(error => {
                resultDiv.className = 'error';
                resultDiv.textContent = '上传出错：' + error.message;
            });
        });
    </script>
</body>
</html>